package com.cloudptt.wp.ui.call.single.fv;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

class FloatingViewTouchListener implements View.OnTouchListener{
    private Context ctx = null;
    private View callView = null;
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private CloseOp op = null;
    private int xInView;
    private int yInView;
    private int xDownInScreen;
    private int yDownInScreen;
    private int xInScreen;
    private int yInScreen;

    public FloatingViewTouchListener(Context ctx,View callView,WindowManager.LayoutParams layoutParams,CloseOp op)
    {
        this.ctx = ctx;
        this.callView = callView;
        this.windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        this.layoutParams = layoutParams;
        this.op = op;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xInView = (int) event.getX();
                yInView = (int) event.getY();

                xDownInScreen = (int) event.getRawX();
                yDownInScreen = (int) event.getRawY();

                xInScreen = (int) event.getRawX();
                yInScreen = (int) event.getRawY();

                break;
            case MotionEvent.ACTION_UP:
                if(xDownInScreen == xInScreen && yDownInScreen == yInScreen)
                {
//                    if(windowManager == null)
//                    {
//                        this.windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
//                    }
//
//                    if(callView != null && callView.isAttachedToWindow()){
//                        Log.debug(this.getClass(), "isAttachedToWindow true");
//                        callView.setOnClickListener(null);
//                        windowManager.removeViewImmediate(callView);
//                    }

                    if(op != null)
                    {
                        op.doClearAndCloseSelf();
                        op.switchToActivity();
                    }
                    //onDestroy();
                }

                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen = (int) event.getRawX();
                yInScreen = (int) event.getRawY();

                if(layoutParams != null)
                {
                    layoutParams.x = (int) (xInScreen - xInView);
                    layoutParams.y = (int) (yInScreen - yInView);

                    windowManager.updateViewLayout(v, layoutParams);
                }
                break;

            default:
                break;
        }

        return true;
    }

}
